package com.leanit.pmc.center.user;

import com.sdeport.security.shiro.SDUtils;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 用户信息管理Controller
 */
@Controller
@RequestMapping("center")
@Scope("prototype")
public class CenterController {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(CenterController.class);

    /**
     * 跳转密码修改页面
     *
     * @param status 1:修改成功；0:修改失败
     * @param map
     * @return
     */
    @RequestMapping(value = "passwordEdit", method = RequestMethod.GET)
    public String passwordEdit(String status, ModelMap map) {
        map.addAttribute("status", status);
        return "center/operator/password_edit";
    }

    /**
     * 跳转用户信息页面
     *
     * @param status
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(value = "operateInfo", method = RequestMethod.GET)
    public String operateInfo(String status, HttpServletRequest request, ModelMap map) {
        map.addAttribute("userInfo", request.getSession().getAttribute("UserInfo"));
        map.addAttribute("status", status);
        return "center/operator/operate_info";
    }

    /**
     * 验证原密码正确性
     *
     * @param password
     * @return
     */
    @RequestMapping(value = "checkOldPassword", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public boolean checkOldPassword(String password) {
        return "12".equals(password);
    }

    /**
     * 更新密码
     *
     * @param response
     * @param request
     */
    @RequestMapping(value = "updatePassword", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePassword(HttpServletResponse response, HttpServletRequest request) {
        String password_old = request.getParameter("password_old");
        String password_new = request.getParameter("password_new");
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("UserInfo");
        try {
            if (SDUtils.updatePassword(userInfo.getLogName(), password_old, password_new)) {
                response.sendRedirect("passwordEdit?status=1");
            } else {
                response.sendRedirect("passwordEdit?status=0");
            }
        } catch (IOException e) {
            logger.error("IOException===", e);
        }
    }

    /**
     * 更新用户信息
     *
     * @param response
     * @param request
     */
    @RequestMapping(value = "updateOperateInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateOperateInfo(HttpServletResponse response, HttpServletRequest request) {
        try {
            response.sendRedirect("operateInfo?status=1");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
